<template>
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="height: 40px; width: 100%"
  >
    <el-col>
      <!-- 左侧内容 -->
      <span>{{ treeNode.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <el-col>{{ treeNode.manager }}</el-col>
        <el-col>
          <!-- 下拉框 , 注册command事件 -->
          <el-dropdown @command="departmentCommand">
            <span class="el-dropdown-link">
              操作<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <!-- 具名插槽 -->
            <el-dropdown-menu slot="dropdown">
              <!-- 下拉框的菜单选项 -->
              <el-dropdown-item command="add" :disabled="!checkPermission('add-dept')">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="edit"
                >编辑部门</el-dropdown-item
              >
              <el-dropdown-item v-if="!isRoot" command="delete"
                >删除部门</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
// 引入API接口
import { deleteDepartmentById } from "@/api/departments";

export default {
  name: "tree-tools",
  props: {
    // 传入的属性,树节点
    treeNode: {
      required: true,
      type: Object,
    },
    isRoot: {
      // 是否根节点
      required: false,
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {};
  },
  created() {},
  mounted() {},
  methods: {
    // 点击下拉框菜单时触发
    departmentCommand(command) {
      if (command === "add") {
        //   添加子部门，通知父组件显示弹出层,使用自定义事件，传递当前节点对象,在当前点击的部门下面添加子部门
        this.$emit("addDept", this.treeNode);
      } else if (command === "edit") {
        //   编辑部门，通知父组件弹出
        this.$emit("editDept", this.treeNode);
      } else {
        //   删除部门,确定进then，取消进catch
        this.$confirm("确定要删除该部门吗？")
          .then(() => {
            // return一个Promise对象
            return deleteDepartmentById(this.treeNode.id);
          })
          .then(() => {
            // 不需要在catch处理失败的情况，已经在request里面处理了
            // 在前端重新拉取最新的数据,让父组件调用接口，自定义事件的方式
            // 在子组件触发 --> 父组件定义的 deleteDepartment事件
            this.$emit("deleteDepartment");
            this.$message.success("删除部门成功");
          });
      }
    },
  },
};
</script>

<style lang="less" scoped>
</style>